package com.shop.front.controller.article;

import com.shop.domain.article.Article;
import com.shop.front.service.article.ArticleService;
import com.shop.web.cache.RedisCache;
import com.shop.web.context.WebContext;
import com.shop.web.response.BaseResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(description = "公告信息", position = -1)
@RestController
@RequestMapping(value = "/article", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private RedisCache redisCache;

    @ApiOperation(value = "公告列表")
    @ApiResponses(@ApiResponse(code = 10000, response = Article.class, message = "content"))
    @RequestMapping("/page")
    public BaseResponse page() {
        Page<Article> page = null;

//        // 公告缓存key
//        int cacheTime = 300;  // 秒
//        String cacheKey = "article_page";
//        // 缓存是否存在
//        String cacheVal = redisCache.getString(cacheKey);
//        if(StringUtils.isNotBlank(cacheVal)) {
//            page = JSON.parseObject(cacheVal, PageImpl.class);
//            return new BaseResponse(page);
//        }

        page = articleService.pageArticle();

        // 缓存返回值
//        redisCache.set(cacheKey, JSON.toJSONString(page), cacheTime);

        return new BaseResponse(page);
    }




    @ApiOperation(value = "查看公告详情")
    @ApiResponses(@ApiResponse(code = 10000, response = String.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "id", paramType = "body"))
    @RequestMapping("/queryArticleInfo")
    public BaseResponse queryArticleInfo() {
        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }
        return articleService.queryArticleInfo(id);
    }


}
